<?php
//require_once 'admin/admin-functions.php';

/*
$facebook = new Facebook(array(
      'appId'  => '624453610898628',
      'secret' => 'f9f4673783061105bc9100feee30616c',
      'cookie' => true,
)); 

$currentUser = $facebook->getUser();

$logoutUrl  = $facebook->getLogoutUrl();

$access_token = $facebook->getAccessToken();

if($currentUser)
    {
        try
        {
            $facebook_profile = $facebook->api('/me/accounts');
            echo '<pre>';
            echo '<a href="'.$logoutUrl.'">admin Log Out</a>';
        }
        catch (FacebookApiException $e)
        {
            print_r($e);
            $user = null;
            $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,offline_access,publish_stream,manage_pages'));
            echo '<a href="'.$loginUrl.'">admin Login</a>';
        }
    }
    else{
        $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,offline_access,publish_stream,manage_pages'));
        echo '<a href="'.$loginUrl.'">admin Login</a>';
    };


function publie_facebook($id_photo, $name_photo, $facebook){
         

   
$msg_body = array(
          'access_token' => 'CAAI37ZBZBoBMQBAMF2pigZAtEoWrUsdOOJpXHH5WCcAbq1V3c9a4eF6mrKPaCZBpHlLiW6f00UMwyzst9Ltyv5jS3DnMMECXTiZCQZBeDwqTrLe52suZBZB271sKvOZBKKG3m6cML0rEHtK4u51Gdl6QWAjNUKFRd8EnIEeTC5zgsmufkhoJWoZBku',
          'message' => 'Une nouvelle photo vient d\'être publié sur le site TOUS ARTISTES',
          'name' => 'Découvrez la galerie des Artistes !',
          'caption' => '',
          'link' => 'http://tousartistes.fr/galerie/',
          'description' => 'Rendez-vous juqu\'au lundi 16 septembre dans les jardins du pavillon Vendôme, 32, rue Celony à Aix en Provence ',
          'picture' => 'http://tousartistes.fr/wp-content/photos/'.$id_photo.'/'.$name_photo,
          'actions' => ''
      );
    

        try
        {
            //$facebook_profile = $facebook->api('/me/accounts');
            $facebook->api('/546258628743594/feed', 'post', $msg_body );

        }
        catch (FacebookApiException $e)
        {
            print_r($e);
            $user = null;
            $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,offline_access,publish_stream,manage_pages'));
            echo '<a href="'.$loginUrl.'">Login</a>';
        }

}

function utilisateur_facebook($id_photo, $name_photo, $access_token_user,$id_facebook,$facebook){



$msg_body = array(
          'access_token' => $access_token_user,
          'message' => 'Une nouvelle photo vient d\'être publié sur le site TOUS ARTISTES',
          'name' => 'Découvrez la galerie des Artistes !',
          'caption' => '',
          'link' => 'http://tousartistes.fr/galerie/',
          'description' => 'Rendez-vous juqu\'au lundi 16 septembre dans les jardins du pavillon Vendôme, 32, rue Celony à Aix en Provence ',
          'picture' => 'http://tousartistes.fr/wp-content/photos/'.$id_photo.'/'.$name_photo,
          'actions' => ''
      );
    
  try
        {
            $facebook_profile = $facebook->api('/me/accounts');
            $facebook->api('/'.$id_facebook.'/feed', 'post', $msg_body );
            echo '<pre>';
            //print_r($facebook_profile);
            echo '<a href="'.$logoutUrl.'">Log Out</a>';
        }
        catch (FacebookApiException $e)
        {
            print_r($e);
            $user = null;
            $loginUrl = $facebook->getLoginUrl(array('scope' => 'email,offline_access,publish_stream,manage_pages'));
            echo '<a href="'.$loginUrl.'">Login</a>';
        }
   

}




require_once(ABSPATH . '/wp-content/plugins/galerie/admin/twitter/codebird.php');
\Codebird\Codebird::setConsumerKey('mHtqRrt5jxujcDdxJqw', 'dG6OK6dNP6LovbJbUEJ8M88ve2Bc92WcmfeVWiosHJY');

$cb = \Codebird\Codebird::getInstance();
$cb->setToken('1516497493-vpaZ1x6MX3PRYg7yuUvsE68k6pIazGBQu7rODuQ','5RJMXb1O832J4lkQitEDQbImvE7YlqlmdGJ32vDqU');



$reply = (array) $cb->statuses_homeTimeline();
$reply = $cb->account_verifyCredentials();

*/

function galerie_getEditItems(){
    $numrows = $_POST['galerie-num-rows'];
    $edit = array();
    for($x=0;$x<$numrows;$x++){
        $rowID = $_POST['cb-'.$x];
        echo $_POST['cb-'.$x];
        if($_POST['cb-'.$rowID] == 'edit'){
            $edit[] = $rowID;
        }
    }
    return $edit;
}

if(isset($_POST['submit-edit'])){
        
   if(isset($_POST['submit-edit'])){
       
    foreach ($_POST['checkbox'] as $value ) {

      $wpdb->update('wp_ja', array('statut'=>$_POST['galerie-action-select']), array('id' => $value), $format = null, $where_format = null );    

      /*if($_POST['galerie-action-select'] == '1'){
        if(isset($_POST['twitter-'.$value])&&!empty($_POST['twitter-'.$value])){
            $description = '@'.$_POST['twitter-'.$value].' a posté une photo sur le site  http://tousartistes.fr/wp-content/photos/'.$value.'/'. $_POST['photo-'.$value];
        }
        else{ $description = 'Nouvelle publication de photo sur le site  http://tousartistes.fr/wp-content/photos/'.$value.'/'. $_POST['photo-'.$value];}
        
         $reply = $cb->statuses_update('status='.$description);
         publie_facebook($value, $_POST['photo-'.$value],$facebook);
         if(isset($_POST['id_facebook-'.$value])&&!empty($_POST['id_facebook-'.$value])){
            utilisateur_facebook($value, $_POST['photo-'.$value], $_POST['access_token-'.$value],$_POST['id_facebook-'.$value],$facebook);
         }
       }*/
     }  
    
    
    
    } 
}

$photos = $wpdb->get_results("SELECT * FROM wp_ja");
?>

<form enctype="multipart/form-data" name="galerie-admin-form" id="galerie-admin-form" action="" method="post">
   <div> 
    <select name="galerie-action-select">
        <option value="1" >Valider</option>
        <option value="0" >Hors ligne</option>
    </select>
   </div>
<div class="wrap">
        <table class="widefat post fixed" style="width:auto;">
            <thead>
                <tr>
                    <th></th>
                    <th>Prénom</th>
                    <th>Nom</th>
                    <th>Email</th>
                    <th>Code Postal</th>
                    <th>Ville</th>
                    <th>Validé</th>
                    <th>Photo</th>
                    <th>Prénom représentant légal</th>
                    <th>Nom représentant légal</th>
                </tr>
            </thead>
            <?php 
                 $i =0;
                 foreach($photos as $photo){
                     //print_r($photo);
                    if(isset($photo->id)){
    
                         $id = $photo->id;
    
                        
                        //$i = 1;
    
                        
            ?>
            <tr>
            <td><input type="checkbox" name="checkbox[]" value="<?php echo $photo->id;?>"><input type="hidden" name="statut-<?php echo $photo->id;?>" value="<?php echo $photo->statut;?>"></td>
            <td><?php echo $photo->prenom;?></td>
            <td><?php echo $photo->nom; ?></td>
            <td><?php echo $photo->email; ?></td>
            <td><?php //echo $photo->cp;?></td>
            <td><?php //echo $photo->ville; ?></td>
            <td><?php echo $photo->statut;?></td>
            <?php
            $dirname = '../wp-content/photos/'.$id.'/';
            
                   if(is_dir($dirname)){
                       $dir = opendir($dirname);
                      while ($fichier = readdir($dir)){  
    
                        //$tab_image[$i] = $fichier;
    
                             if(is_file($dirname.$fichier)) {
                              $width = 300;
                              $image =getimagesize($dirname.'/'.$fichier);
                              $reduction = (($width * 100)/$image[0] );
                              $height = ( ($image[1] * $reduction)/100 );
            ?>
            <td><img width="300px" height="<?php echo $height; ?>" src="<?php echo '../wp-content/photos/'.$id.'/'.$fichier; ?>" /><input type="hidden" name="photo-<?php echo $id;?>" value="<?php echo $fichier;?>"></td>
            <input type="hidden" name="id_facebook-<?php echo $id;?>" value="<?php echo $photo->id_facebook; ?>" />
            <input type="hidden" name="access_token-<?php echo $id;?>" value="<?php echo $photo->token_facebook; ?>" />
            <input type="hidden" name="twitter-<?php echo $id;?>" value="<?php echo $photo->twitter; ?>" />
            <?php 
                            }                           
                       }
                   }?>
            <td><?php echo $photo->prenom_legal;?></td>
            <td><?php echo $photo->nom_legal; ?></td>
           </tr>
                <?php   }
                  $i++;
                }?>
        </table>
        <input type="submit" name="submit-edit" id="submit-edit" class="button-primary" value="Sauvegarder" />
        <input type="hidden" name="galerie-num-rows" id="galerie-num-rows" value="<?php echo $i;?>" />
        
</div>
</form>
